package com.lumlord.service;

import com.lumlord.common.annotation.redis.Cache;
import com.lumlord.common.cache.CacheCenter.Type;
import com.lumlord.common.domain.Project;
import com.lumlord.common.exception.BusinessException;
import com.lumlord.mapper.custom.CommonDao;
import com.lumlord.model.auto.dictionary.Dictionary;
import com.lumlord.util.JdbcTemplateUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * @Classname CommonService1
 * @description: 提供一些公共的方法调用
 * @author: pengdingding
 * @create: 2020-04-29 13:59
 * @Version V1.0（版本号）
 **/
@Service
public class CommonService {
	
	 @Cache(cacheFamily=Dictionary.class,key="#id",type=Type.cover)
	 public Dictionary selectByDicIdUse(Integer id) throws BusinessException{
		 return jdbcTemplateUtils.load(Dictionary.class, id);
	 }
	 @Cache(cacheFamily=Dictionary.class,key="#id")
	 public Dictionary selectByDicId(Dictionary a) throws BusinessException{
		 return jdbcTemplateUtils.queryOne(a);
	 }
	 public Project selectProjectById(Integer id) throws BusinessException{
		 return jdbcTemplateUtils.load(Project.class,id);
	 }

	 @Cache(cacheFamily=Dictionary.class,key="#filedName")
	 public Dictionary selectByDicId(String filedName) throws BusinessException{
		 Dictionary a = new Dictionary();
		 a.setFieldName(filedName);
		 return jdbcTemplateUtils.queryOne(a);
	 }
	 
	 
	 public void test() throws BusinessException{
		  jdbcTemplateUtils.queryOne("slect 1 from dual where 1=?", Dictionary.class, 1);
	 }
	 
    /**
     * 获取一条字典数据
     * @param id 当前字典数据的id
     * @return
     */
    public Map<String, Object> selectByDicId(int id){
        Map<String, Object> map = commonDao.selectDicById(id);
        if (ObjectUtils.isEmpty(map)){
            return null;
        }else {
            return map;
        }
    }
       /**
     * 获取一条字典数据
     * @param filedName 当前字典数据的字段
     * @return
     */
    public Map<String, Object> selectByFieldName(String filedName){
        Map<String, Object> map = commonDao.selectNowDicByName(filedName);
        if (ObjectUtils.isEmpty(map)){
            return null;
        }else {
            return map;
        }
    }

 /**
     * 获取下级字典数据
     * @param filedName 当前字典数据的字段
     * @return
     */
    public List<Map<String, Object>> selectNextByFieldName(String filedName){
        Map<String, Object> map = commonDao.selectNowDicByName(filedName);
        if (ObjectUtils.isEmpty(map)){
            return null;
        }else {
            List<Map<String, Object>> list = commonDao.selectNextDicById((int) map.get("id"));
            return list;
        }
    }


    /**
     * 劳务人员id查询信息
     * @param id 劳务人员id
     * @return
     */
    public Map<String, Object> selectLabById(Integer id){
        Map<String, Object> map = commonDao.selectLaborName(id);
        if (!ObjectUtils.isEmpty(map)){
            Object workType = map.get("workType");
            if (!ObjectUtils.isEmpty(workType)){
                Map<String, Object> map1 = commonDao.selectDicById((Integer) workType);
                if (!ObjectUtils.isEmpty(map1)){
                    Object val = map1.get("val");
                    map.put("workTypeName",val);
                }
            }
        }
        return map;
    }




    

    @Autowired
    private CommonDao commonDao;
    
    @Autowired
	JdbcTemplateUtils jdbcTemplateUtils;
    
}
